/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.pi;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import researchgrants.utils.Constants;
import researchgrants.parts.LoggedData.panels.LoggedDataPanels;
import javax.swing.event.ChangeEvent;

/**
 *
 * @author DOStudent1
 */
public class LoggedDataPanelsPis extends LoggedDataPanels implements ListSelectionListener {

    PisList pisList;
    
    @Override
    public Component getViewComponent() {
        checkInitializedData();
        populatePi();
        JPanel pnlPis=new JPanel(new GridLayout(0, 1));
        for (Iterator<Pi> it = pisList.iterator(); it.hasNext();) {
            Pi pi = it.next();

            JPanel pnlPi=new JPanel(new FlowLayout(FlowLayout.LEFT, 0,0));

            JButton btnViewPi=new JButton("View");
            btnViewPi.setMargin(Constants.smallButtonMargins);
            btnViewPi.setFont(Constants.smallButtonFont);
            btnViewPi.addActionListener(new ViewPiActionListener(getParentWindow(),pi));
            pnlPi.add(btnViewPi);
            
            JLabel lblPi=new JLabel(pi.getShortForm());
            pnlPi.add(lblPi);
            
            pnlPis.add(pnlPi);
        }
        return(pnlPis);
    }

    JList lstPis;
    JPanel pnlEditContent=new JPanel(new BorderLayout(0, 0));
    
    @Override
    public Component getEditComponent() {

        createPisList(null);
        
        
        return(pnlEditContent);
    }
    
    private void createPisList(Integer selectedId) {
        pnlEditContent.removeAll();
        
        lstPis=new JList();
        JScrollPane scrollpane=new JScrollPane(lstPis, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED ,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        checkInitializedData();
        lstPis.addListSelectionListener(this);

        // fires the first value change to initially set the data validation
        this.valueChanged(null);

        lstPis.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        DefaultListModel defModel = new DefaultListModel();
        lstPis.setModel (defModel);

        PisList allPis=AllPis.getAllPis();
        Iterator<Pi> allPisIterator=allPis.iterator();
        
        int i=0;
        while (allPisIterator.hasNext()) {
            Pi pi = allPisIterator.next();
            defModel.addElement(pi.getShortForm());
            i++;
        }

        if (hasDataInitialized()) {
            populatePi();
            i=0;
            allPisIterator=allPis.iterator();
            while (allPisIterator.hasNext()) {
                Pi pi = allPisIterator.next();
                if (pisList.contains(pi)) {
    //                lstPis.addSelectionInterval(i, i);
    //                lstPis.setSelectedIndices(new int[2]{0,1});
                    lstPis.addSelectionInterval(i, i);
                }
                i++;
            }
        }
        
        if (selectedId!=null) {
            i=0;
            allPisIterator=allPis.iterator();
            while (allPisIterator.hasNext()) {
                Pi pi = allPisIterator.next();
                if (selectedId.equals(new Integer(pi.getId()))) {
    //                lstPis.addSelectionInterval(i, i);
    //                lstPis.setSelectedIndices(new int[2]{0,1});
                    lstPis.addSelectionInterval(i, i);
                    lstPis.ensureIndexIsVisible(i);
                }
                i++;
            }
        }
//        lstPis.setMaximumSize(new Dimension(0, 200));
//        lstPis.setPreferredSize(new Dimension(0, 200));
//        lstPis.setAutoscrolls(true);
        lstPis.setVisibleRowCount(4);
        
//        lstPis.addSelectionInterval(0, 0);
//        lstPis.addSelectionInterval(0, 1);
//        lstPis.getSelectionModel().setAnchorSelectionIndex(0);
//        lstPis.getSelectionModel().setLeadSelectionIndex(1);
//        lstPis.getSelectionModel().;
//        ////        lstPis.setSelectedIndices(selects);
        
//        CheckList lstPis=new CheckList();
//        lstPis.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        
        
        pnlEditContent.add(scrollpane, BorderLayout.CENTER);
//        pnlEditContent.add(lstPis, BorderLayout.CENTER);
        
        JButton btnCreatePi=new JButton("New Pi");
        btnCreatePi.addActionListener(new NewPiActionListener(getParentWindow()));
        btnCreatePi.setMargin(Constants.smallButtonMargins);
        btnCreatePi.setFont(Constants.smallButtonFont);
        pnlEditContent.add(btnCreatePi, BorderLayout.SOUTH);
        

    }

    @Override
    public String getEdittedValue() {
        return(getEdittedValueInner());
    }
    private String getEdittedValueInner() {
        PisList allPis=AllPis.getAllPis();
        Iterator<Pi> allPisIterator=allPis.iterator();
        int i=0;
        
        PisList newPis=new PisList();
        while (allPisIterator.hasNext()) {
            Pi pi = allPisIterator.next();
            if (lstPis.isSelectedIndex(i)) {
                newPis.add(pi);
            }
            i++;
        }
        return(newPis.toDelimitedPisList());
    }

    private void checkInitializedData() {
        if (!isParentInitialized()) {
            throw new RuntimeException("Didn't set parent");
        }
    }

    private void populatePi() {
        pisList=PisList.fromDelimitedPisList(getData().value());
    }

    private class ViewPiActionListener implements ActionListener {

        private final Pi pi;
        Window parentWindow;

        public ViewPiActionListener(Window parent, Pi pi) {
            this.pi = pi;
            this.parentWindow=parent;
        }

        public void actionPerformed(ActionEvent e) {
            pi.show(parentWindow);
//            getParentComponent().display();
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        ChangeEvent eNew=new ChangeEvent(lstPis);
        notifyAllChangeListeners(eNew);
    }

    @Override
    public boolean isEmpty() {
        return(lstPis.getSelectedIndices().length==0);
    }
    
    @Override
    public boolean isValid() {
        return(true);
    }



    private class NewPiActionListener implements ActionListener {

        Window parent;
        
        public NewPiActionListener(Window parent) {
            this.parent=parent;
        }

        public void actionPerformed(ActionEvent e) {
            Pi newPi=AllPis.inputNewPi(parent);
            if (newPi!=null) {
                createPisList(newPi.getId());
                lstPis.revalidate();
                pnlEditContent.revalidate();
            }
        }
    }


}
